const mongoose = require('mongoose')
const Setting = mongoose.model('setting')
const { formatBody } = require('../../tools')

module.exports = {
  async setter (ctx) {
    let json = ctx.request.body
    let Arr = await Setting.find({})
    let setting
    if (Arr.length > 0) {
      _id = Arr[0]._id
      await Setting.updateOne({ _id }, json)
    } else {
      setting = new Setting(json)
      await setting.save()
    }
    return ctx.body = formatBody(1, 'ok')
  },
  async getter (ctx) {
    let Arr = await Setting.find({})
    let data = {}
    if (Arr.length > 0) data = Arr[0]
    return ctx.body = formatBody(1, 'ok', data)
  }
}